//
//  BIDBaseViewController.m
//  EducationAssistant
//
//  Created by mal on 15/6/2.
//  Copyright (c) 2015年 cnsoft. All rights reserved.
//

#import "BIDBaseViewController.h"
#import "UIView+FindFirstResponder.h"

@interface BIDBaseViewController ()
{
    UIView *_hudView;
    UIActivityIndicatorView *_spinnerView;
    UIView *_maskView;
}
@end

@implementation BIDBaseViewController
@synthesize bNetworkConnecting;
@synthesize toolBar;
@synthesize distanceToMove;
@synthesize keyboardHeight;
@synthesize networkConnectionArr;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    //[self.view setBackgroundColor:RGBACOLOR(230.f, 230.f, 237.f, 1.f)];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self initToolBarForKeyboard];
    networkConnectionArr = [[NSMutableArray alloc] init];
    //定义导航栏返回按钮
    //self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnHandler)];
    //UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnHandler)];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(backBtnHandler)];
    self.navigationItem.backBarButtonItem = barButtonItem;
    //
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    _hudView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50.f, 50.f)];
    [_hudView setBackgroundColor:[UIColor colorWithWhite:0.2f alpha:0.5f]];
    _spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    CGRect frame = _spinnerView.frame;
    frame.origin.x = (CGRectGetWidth(_hudView.frame)-frame.size.width)/2;
    frame.origin.y = (CGRectGetHeight(_hudView.frame)-frame.size.height)/2;
    _spinnerView.frame = frame;
    //
    frame = _hudView.frame;
    frame.origin.x = (screenSize.width-frame.size.width)/2;
    frame.origin.y = (screenSize.height-frame.size.height)/2-64;
    _hudView.frame = frame;
    _hudView.clipsToBounds = YES;
    _hudView.layer.cornerRadius = 5.f;
    [_hudView addSubview:_spinnerView];
    //
    _maskView = [[UIView alloc] init];
    _maskView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
}

- (void)initToolBarForKeyboard
{
    //toolBar
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGSize toolBarSize = CGSizeMake(screenSize.width, 44);
    CGRect toolBarFrame = CGRectMake(0, 0, toolBarSize.width, toolBarSize.height);
    toolBar = [[UIToolbar alloc] initWithFrame:toolBarFrame];
    toolBar.tintColor = [UIColor whiteColor];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"关闭键盘" style:UIBarButtonItemStylePlain target:self action:@selector(closeKeyboardHandler)];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolBar setItems:[[NSArray alloc] initWithObjects:item2, item1, nil]];
    toolBar.barStyle = UIBarStyleBlack;
    toolBar.translucent = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backBtnHandler
{
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if(self.navigationController)
    {
        self.navigationController.navigationBarHidden = NO;
    }
    //监听键盘
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    UINavigationController *navController = self.navigationController;
    if(navController)
    {
        if([navController.viewControllers containsObject:self])
        {
        }
        else
        {
            //取消正在进行的网络请求
            for(NSURLConnection *connection in networkConnectionArr)
            {
                if(connection) [connection cancel];
            }
            [networkConnectionArr removeAllObjects];
            //执行点击返回按钮时需要执行的操作
            [self backBtnHandler];
        }
    }
}

- (void)showSpinnerView
{
    if(_spinnerView)
    {
        [self.view addSubview:_maskView];
        [self.view addSubview:_hudView];
        [_spinnerView startAnimating];
    }
}

- (void)hideSpinnerView
{
    if(_spinnerView)
    {
        [_maskView removeFromSuperview];
        [_spinnerView stopAnimating];
        [_hudView removeFromSuperview];
    }
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    keyboardHeight = keyboardSize.height;
    
    //NSValue *durationValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *firstResponderView = [keyWindow findFirstResponder];
    
    if(firstResponderView.frame.origin.y + firstResponderView.frame.size.height > (self.view.frame.size.height-(keyboardHeight-distanceToMove)))
    {
        //NSTimeInterval timeInterval;
        //[durationValue getValue:&timeInterval];
        [UIView beginAnimations:@"animation" context:nil];
        [UIView setAnimationDuration:0.25f];
        CGRect frame = self.view.frame;
        
        int moveLen = firstResponderView.frame.origin.y + firstResponderView.frame.size.height - (self.view.frame.size.height-(keyboardHeight-distanceToMove));
        distanceToMove += moveLen;
        frame.origin.y -= moveLen;
        self.view.frame = frame;
        [UIView commitAnimations];
    }
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.30f];
    CGRect frame = self.view.frame;
    frame.origin.y += distanceToMove;
    self.view.frame = frame;
    distanceToMove = 0;
    [UIView commitAnimations];
}
//
- (void)closeKeyboardHandler
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    //UIView *firstResponderView = [keyWindow.rootViewController.view findFirstResponder];
    UIView *firstResponderView = [keyWindow findFirstResponder];
    [firstResponderView resignFirstResponder];
}

- (void)setNavigationControllerAttribute:(UINavigationController *)navController
{
    [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBg.png"] forBarMetrics:UIBarMetricsDefault];
    NSDictionary *attributeDictionary = @{NSFontAttributeName:[UIFont systemFontOfSize:22.0f], NSForegroundColorAttributeName:[UIColor whiteColor]};
    [navController.navigationBar setTitleTextAttributes:attributeDictionary];
    [navController.navigationBar setTintColor:[UIColor whiteColor]];
}

@end
